/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerCategoryManager;
/*     */ import com.newcrm.db.DealCategoryManager;
/*     */ import com.newcrm.db.GroupManager;
/*     */ import com.newcrm.db.TaskCategoryManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.sql.SQLException;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class AppLogin extends AbstractAction
/*     */ {
/*  35 */   private static Logger logger = Logger.getLogger(AppLogin.class.getName());
/*     */ 
/*  37 */   private String errorMsg = "";
/*     */ 
/*     */   public AppLogin(AbstractHttpServlet servlet) {
/*  40 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  46 */     boolean flag = doLogin(request, response);
/*  47 */     if (!flag) {
/*  48 */       super.showErrorMessage(response, this.errorMsg);
/*  49 */       return;
/*     */     }
/*     */ 
/*  52 */     response.setContentType("text/html;charset=utf-8");
/*  53 */     PrintWriter out = response.getWriter();
/*  54 */     JSONObject jsonObject = new JSONObject();
/*  55 */     jsonObject.put("error", "0");
/*  56 */     jsonObject.put("user", super.getLoginUser(request));
/*  57 */     out.println(jsonObject);
/*  58 */     out.flush();
/*  59 */     out.close();
/*     */   }
/*     */ 
/*     */   private boolean doLogin(HttpServletRequest request, HttpServletResponse response)
/*     */   {
/*  73 */     String userId = Util.getURLParameter(request.getParameter("userId"));
/*  74 */     String password = Util.getURLParameter(request.getParameter("pwd"));
/*     */ 
/*  76 */     if ((userId == null) || (userId.trim().length() == 0)) {
/*  77 */       this.errorMsg = "请输入用户名";
/*  78 */       return false;
/*     */     }
/*     */ 
/*  81 */     if ((password == null) || (password.trim().length() == 0)) {
/*  82 */       this.errorMsg = "请输入登陆密码";
/*  83 */       return false;
/*     */     }
/*     */ 
/*  86 */     UserManager manager = new UserManager();
/*     */ 
/*  88 */     Integer companyId = null;
/*     */     try {
/*  90 */       Map map = manager.login(userId, password, true);
/*  91 */       companyId = (Integer)map.get("companyId");
/*     */     } catch (Exception e) {
/*  93 */       logger.warn("login failed", e);
/*  94 */       this.errorMsg = "用户名或密码输入错误";
/*  95 */       return false;
/*     */     }
/*  97 */     if ((companyId == null) || (companyId.intValue() < 0)) {
/*  98 */       this.errorMsg = "用户名或密码输入错误";
/*  99 */       return false;
/*     */     }
/*     */ 
/* 103 */     String ip = Util.getClientIpAddr(request);
/*     */     try {
/* 105 */       manager.updateLogin(userId, ip);
/*     */     } catch (Exception e) {
/* 107 */       logger.warn("update login info failed", e);
/*     */     }
/*     */ 
/* 111 */     Map companys = (Map)this.servlet
/* 112 */       .getServletContext().getAttribute("companys");
/*     */ 
/* 114 */     if (companys == null) {
/* 115 */       companys = new HashMap();
/* 116 */       this.servlet.getServletContext().setAttribute(
/* 117 */         "companys", companys);
/*     */     }
/*     */ 
/* 120 */     CompanyInfo company = (CompanyInfo)companys.get(companyId);
/* 121 */     if (company == null) {
/* 122 */       company = new CompanyInfo();
/* 123 */       company.id = companyId.intValue();
/* 124 */       companys.put(companyId, company);
/*     */     }CustomerCategoryManager customerCategoryManager;
/*     */     try {
/* 127 */       if (company.users == null)
/*     */       {
/* 129 */         company.users = manager.getCompanyUsers(companyId.intValue(), 
/* 130 */           null);
/*     */ 
/* 133 */         TaskCategoryManager taskCategoryManager = new TaskCategoryManager();
/* 134 */         DealCategoryManager dealCategoryManager = new DealCategoryManager();
/* 135 */         customerCategoryManager = new CustomerCategoryManager();
/* 136 */         GroupManager groupManager = new GroupManager();
/*     */         try {
/* 138 */           company.taskCategorys = taskCategoryManager.list(companyId
/* 139 */             .intValue());
/* 140 */           company.dealCategorys = dealCategoryManager.list(companyId
/* 141 */             .intValue());
/* 142 */           company.customerCategorys = customerCategoryManager.list(
/* 143 */             companyId.intValue(), 1);
/*     */ 
/* 145 */           company.setGroups(groupManager.list(companyId.intValue()));
/*     */         }
/*     */         catch (SQLException e) {
/* 148 */           logger.warn("get company task/deal/group category failed", 
/* 149 */             e);
/*     */         }
/*     */       }
/*     */     } catch (DBException e) {
/* 153 */       logger.warn("get company users failed", e);
/* 154 */       this.errorMsg = "用户名或密码输入错误";
/* 155 */       return false;
/*     */     }
/* 157 */     User currentUser = null;
/* 158 */     for (User user : company.users) {
/* 159 */       if (userId.equals(user.userId)) {
/* 160 */         currentUser = user;
/* 161 */         break;
/*     */       }
/*     */     }
/*     */ 
/* 165 */     if (currentUser == null) {
/* 166 */       this.errorMsg = "用户名或密码输入错误";
/* 167 */       return false;
/*     */     }
/*     */ 
/* 170 */     HttpSession session = request.getSession(true);
/* 171 */     session.setAttribute("user", currentUser);
/* 172 */     return true;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.AppLogin
 * JD-Core Version:    0.6.1
 */